package messaging.format;

import java.util.Hashtable;

// - Created on 10 October 2008
// ----------------------------
// - For adding additional information to existing messaging format
// - Include Parser, Adding of new options values and types

public class MsgExtension {

	Hashtable<String, String> additionalInformation = new Hashtable<String, String>();
	
	public MsgExtension(){
			
	}
	
	// - Extend the Message to include another Variable.
	public void addVariable(String varName, String value){
		
		additionalInformation.put(varName, value);
		
	}
	
	// - Reset the extension to empty string.-
	public void reset(){
		
		additionalInformation = new Hashtable<String, String>();
		
	}

	// - Get the value of the Variable.
	public String getValue(String varName){
		
		if (additionalInformation.containsKey(varName))
			
			return additionalInformation.get(varName);
			
		else
			
			return null;
				
		
	}
	
	// - Parse the additionalString and load it to Environment.
	public void LoadAdditionalString(String additionalString){
		
		String as = additionalString;
		String record;
		int ptr = 1;
		
		// - Traverse for each Additional Option.
		while (as.indexOf("?",ptr)>=0){
		
			record = as.substring(0, as.indexOf("?",ptr));
			as = as.substring(as.indexOf("?",ptr)+1);			
			
			String newVar = record.substring(0, record.indexOf("="));
			
			String newValue = record.substring(record.indexOf("=")+1,record.length());
			
			addVariable(newVar, newValue);
			
		}
		
	}
	
	// - Output the resulting String representation of the MsgExtension.+-
	public String toString(){
		
		String res = "";
		
		for (int i=0; i<additionalInformation.keySet().toArray().length; i++){
			
			res+=additionalInformation.keySet().toArray()[i] +"="+additionalInformation.get(additionalInformation.keySet().toArray()[i])+"?";
			
		}
		
		return res;
		
	}
	
}
